package com.wangjiyuan.giftshuo.ui.fragment.classify.shop;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.classifybean.shop.TreeBean;
import com.wangjiyuan.giftshuo.ui.fragment.classify.shop.adapter.ShopContentRecyclerViewAdapter;
import com.wangjiyuan.giftshuo.ui.fragment.classify.shop.adapter.ShopTreeBaseAdapter;

import java.util.ArrayList;
import java.util.List;

import static android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING;

/**
 * A simple {@link Fragment} subclass.
 * 单品的界面
 */
public class ShopFragment extends Fragment implements ShopContract.IShopView{
    int treePosition;

    private Context mContext;
    private ListView listView_shop_tree;
    private List<TreeBean.DataBean.CategoriesBean> list_tree;
    private ShopTreeBaseAdapter treeAdapter;

    private RecyclerView recyclerView_shop_content;
    private List<TreeBean.DataBean.CategoriesBean> list_content;
    private ShopContentRecyclerViewAdapter contentAdapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext=getContext();
        list_tree =new ArrayList<>();
        list_content=new ArrayList<>();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShopPresenter shopPresenter=new ShopPresenter(this);
        shopPresenter.getTreeObservable();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shop, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView_shop_tree = (ListView) view.findViewById(R.id.listView_shop_tree);
        recyclerView_shop_content= (RecyclerView) view.findViewById(R.id.recyclerView_shop_content);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        treeAdapter =new ShopTreeBaseAdapter(mContext, list_tree);
        listView_shop_tree.setAdapter(treeAdapter);

        listView_shop_tree.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                将ListView中的item的位置传给适配器
                treeAdapter.clearPosition(position);
//                刷新适配器
                treeAdapter.notifyDataSetChanged();

                listView_shop_tree.setSelection(position-1);
                treePosition=position;
                recyclerView_shop_content.scrollToPosition(treePosition);
            }
        });

        final LinearLayoutManager linearLayoutManager_content=new LinearLayoutManager(mContext, OrientationHelper.VERTICAL,false);
        contentAdapter=new ShopContentRecyclerViewAdapter(mContext,list_content);
        recyclerView_shop_content.setHasFixedSize(true);
        recyclerView_shop_content.setLayoutManager(linearLayoutManager_content);
        recyclerView_shop_content.setAdapter(contentAdapter);
        recyclerView_shop_content.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int firstVisibleItemPosition=0;
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE:
//                        获得当前显示在第一个item的位置
                        firstVisibleItemPosition = linearLayoutManager_content.findFirstVisibleItemPosition();
                        treeAdapter.clearPosition(firstVisibleItemPosition);
                        treeAdapter.notifyDataSetChanged();
                        listView_shop_tree.setSelection(firstVisibleItemPosition-1);
                        break;

                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        firstVisibleItemPosition = linearLayoutManager_content.findFirstVisibleItemPosition();
                        treeAdapter.clearPosition(firstVisibleItemPosition);
                        treeAdapter.notifyDataSetChanged();
                        listView_shop_tree.setSelection(firstVisibleItemPosition-1);
                        break;

                    case RecyclerView.SCROLL_STATE_SETTLING:
                        break;
                }
            }
        });
    }

    @Override
    public void loadTreeData(TreeBean bean) {
        List<TreeBean.DataBean.CategoriesBean> categories = bean.getData().getCategories();
        list_tree.addAll(categories);
        treeAdapter.notifyDataSetChanged();

        contentAdapter.reloadRecyclerView(categories,true);
    }
}
